#!/bin/bash
IMG_NAME=nos
TAG=latest

ROOT_DIR="/opt/jenkins/docker/${IMG_NAME}"

#!/bin/bash
set -x
ROOT_DIR="/opt/jenkins/docker/${INAME}"
install_pkg()
{
sudo rm -fr $ROOT_DIR
sudo mkdir -p $ROOT_DIR/{bin,dev,etc,lib64,proc,sys,usr}
sudo mkdir -p $ROOT_DIR/usr/bin
#sudo chown jenkins:jenkins $ROOT_DIR

sudo mknod -m 600 $ROOT_DIR/dev/console c 5 1
sudo mknod -m 666 $ROOT_DIR/dev/null c 1 3

sudo cp /etc/shells $ROOT_DIR/etc/
sudo cp /bin/bash $ROOT_DIR/usr/bin
sudo cp /usr/bin/rm $ROOT_DIR/usr/bin
sudo cp /usr/bin/mv $ROOT_DIR/usr/bin
sudo cp /usr/bin/dirname $ROOT_DIR/usr/bin
sudo cp /usr/bin/sed $ROOT_DIR/usr/bin
sudo cp /usr/bin/sort /$ROOT_DIR/usr/bin
sudo cp /usr/bin/tail $ROOT_DIR/usr/bin
sudo cp /usr/bin/mkdir $ROOT_DIR/usr/bin
sudo cp /usr/bin/ln $ROOT_DIR/usr/bin
sudo cp /usr/bin/grep $ROOT_DIR/usr/bin
sudo cp /usr/bin/head $ROOT_DIR/usr/bin

sudo cp /lib64/ld-linux-x86-64.so.2 $ROOT_DIR/lib64/
sudo ldd /usr/bin/bash | awk '/=>/ {print $3}' | xargs -I {} sudo cp {} $ROOT_DIR/lib64/
sudo cp /usr/lib64/libselinux.so.1 $ROOT_DIR/lib64/
sudo cp /usr/lib64/libpcre2-8.so.0 $ROOT_DIR/lib64/
sudo cp /usr/lib64/libacl.so.1 $ROOT_DIR/lib64/
sudo cp /usr/lib64/libattr.so.1 $ROOT_DIR/lib64/

sudo ln -s /usr/bin/bash $ROOT_DIR/bin/sh
sudo mkdir -p $ROOT_DIR/var/volatile/tmp
sudo ln -s /var/volatile/tmp $ROOT_DIR/var/tmp
tree $ROOT_DIR


    export LD_LIBRARY_PATH=/lib64:/usr/lib64

    sudo -E dnf install --installroot=$ROOT_DIR --releasever=1.0.0 -y base-files

    sudo mv $ROOT_DIR/usr/bin/bash.bash $ROOT_DIR/usr/bin/bash
    sudo dnf install --installroot=$ROOT_DIR --releasever=1.0.0 -y coreutils busybox  rpm os-release ethtool dnf jq util-linux-uuidgen openssl
    sudo dnf install --installroot=$ROOT_DIR --releasever=1.0.0 -y dnf-alt
}

clean_up()
{
    for file in "$ROOT_DIR/usr/bin"/*; do
        if [ -f "$file" ]; then
            base_name=$(basename "$file")
            if [[ "$base_name" == *.coreutils ]]; then
                new_name="${base_name%.coreutils}"
                new_file="$ROOT_DIR/usr/bin/$new_name"
                sudo mv "$file" "$new_file"
            fi
        fi
    done
}



post()
{
    cd /opt/jenkins/docker
    sudo docker import ${INAME}.tar.gz ${INAME}:${TAG}
    docker tag  ${INAME}:${TAG}  registry.com/nos/${INAME}:${TAG}
    echo "pass123$" | docker login --username=k8s --password-stdin registry.com
    docker push registry.com/nos/${INAME}:${TAG}
}


main()
{
    install_pkg
    set +x
    clean_up
    set -x

    cd ${ROOT_DIR}
    sudo tar -czf ../${INAME}.tar.gz .
}



